/**
 * 
 */
package com.cqan.entity.account;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.*;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.cqan.entity.pk.IdEntity;

/**
 * 
 * 
 * 用户实体
 * 
 * @author wuhui
 * 
 *         下午02:59:51
 */
@Entity
// 表名与类名不相同时重新定义表名.
@Table(name = "SYS_USER")
// 默认的缓存策略.
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User extends IdEntity implements UserDetails {

	/**
	 * 真实姓名
	 */
	private String realName;

	/**
	 * 昵称
	 */
	private String nickName;

	/**
	 * 登陆名
	 */
	private String loginName;

	/**
	 * 密码
	 */
	private String password;

	/**
	 * 邮件地址
	 */
	private String email;

	/**
	 * 生日
	 */
	private Date birthday;

	/**
	 * 注册时间
	 */
	private Date createTime;

	/**
	 * 状态 0:正常，1:锁定
	 */
	private Boolean status;

	/**
	 * 地址
	 */
	private String address;

	/**
	 * 电话
	 */
	private String phone;

	/**
	 * 角色
	 */
	private List<Role> roles;

	/**
	 * 所属部门
	 */
	private List<Organization> orgs;

	/**
	 * 注册IP
	 */
	private String regip;

	/**
	 * 最一次登陆IP
	 */
	private String logip;

	/**
	 * 最后一次登陆时间
	 */
	private Date lastLoginTime;

	/**
	 * 是否激活 true：激活，false：未激活
	 */
	private Boolean activation;

	/**
	 * 头像
	 * 
	 * @return
	 */
	private String avatar;
	
	/**
	 * 权限集合
	 */
	private List<GrantedAuthority> authorities;

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public Boolean getActivation() {
		return activation;
	}

	public void setActivation(Boolean activation) {
		this.activation = activation;
	}

	public String getRegip() {
		return regip;
	}

	public void setRegip(String regip) {
		this.regip = regip;
	}

	public String getLogip() {
		return logip;
	}

	public void setLogip(String logip) {
		this.logip = logip;
	}

	public Date getLastLoginTime() {
		return lastLoginTime;
	}

	public void setLastLoginTime(Date lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	public String getAddress() {
		return address;
	}

	public Date getBirthday() {
		return birthday;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public String getEmail() {
		return email;
	}

	public String getLoginName() {
		return loginName;
	}

	// 多对多定义
	@ManyToMany
	// 中间表定义,表名采用默认命名规则
	@JoinTable(name = "SYS_USER_ORG", joinColumns = { @JoinColumn(name = "uid") }, inverseJoinColumns = { @JoinColumn(name = "orgid") })
	// Fecth策略定义
	@Fetch(FetchMode.SUBSELECT)
	// 集合按id排序.
	@OrderBy("id")
	// 集合中对象id的缓存.
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Organization> getOrgs() {
		return orgs;
	}

	public String getPassword() {
		return password;
	}

	public String getPhone() {
		return phone;
	}

	public String getRealName() {
		return realName;
	}

	// 多对多定义
	@ManyToMany
	// 中间表定义,表名采用默认命名规则
	@JoinTable(name = "SYS_USER_ROLE", joinColumns = { @JoinColumn(name = "uid") }, inverseJoinColumns = { @JoinColumn(name = "rid") })
	// Fecth策略定义
	@Fetch(FetchMode.SUBSELECT)
	// 集合按id排序.
	@OrderBy("id")
	// 集合中对象id的缓存.
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Role> getRoles() {
		return roles;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public void setOrgs(List<Organization> orgs) {
		this.orgs = orgs;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public Boolean getStatus() {
		return status;
	}

	public void setStatus(Boolean status) {
		this.status = status;
	}

	/**
	 * @return
	 * @see org.springframework.security.core.userdetails.UserDetails#getAuthorities()
	 */
	@Transient
	@Override
	public List<GrantedAuthority> getAuthorities() {
		
		return authorities;
	}
	
	

	public void setAuthorities(List<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}

	/**
	 * @return
	 * @see org.springframework.security.core.userdetails.UserDetails#getUsername()
	 */
	@Transient 
	@Override
	public String getUsername() {
		
		return loginName;
	}

	/**
	 * @return
	 * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonExpired()
	 */
	@Transient 
	@Override
	public boolean isAccountNonExpired() {
		
		return true;
	}

	/**
	 * @return
	 * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonLocked()
	 */
	@Transient 
	@Override
	public boolean isAccountNonLocked() {
		
		return true;
	}

	/**
	 * @return
	 * @see org.springframework.security.core.userdetails.UserDetails#isCredentialsNonExpired()
	 */
	@Transient 
	@Override
	public boolean isCredentialsNonExpired() {
		
		return true;
	}

	/**
	 * @return
	 * @see org.springframework.security.core.userdetails.UserDetails#isEnabled()
	 */
	@Transient 
	@Override
	public boolean isEnabled() {
		
		return getActivation();
	}

}
